package com.example.mygame;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class StateActivity extends AppCompatActivity {

    private TextView textViewname;
    private TextView textViewpwd;

    private ProgressBar progressBar1;
    private ProgressBar progressBar2;
    private ProgressBar progressBar3;
    private Button buttonbuy;
    private Button buttonclear;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_state);

        textViewname=findViewById(R.id.textViewname);
        textViewpwd=findViewById(R.id.textViewpwd);
        Intent intent=getIntent();
        textViewname.setText(intent.getStringExtra("userName"));
        textViewpwd.setText(intent.getStringExtra("userPWD"));

        progressBar1=findViewById(R.id.progressBar1);
        progressBar2=findViewById(R.id.progressBar2);
        progressBar3=findViewById(R.id.progressBar3);
        buttonbuy=findViewById(R.id.buttonbuy);
        buttonclear=findViewById(R.id.buttonclear);

        buttonbuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1=new Intent(StateActivity.this,ShopActivity.class);
                startActivityForResult(intent1,1);

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Weapon weapon=(Weapon) data.getSerializableExtra("weapon");
        progressBar1.setProgress(progressBar1.getProgress()+weapon.getAttack());
        progressBar2.setProgress(progressBar2.getProgress()+weapon.getDefence());
        progressBar3.setProgress(progressBar3.getProgress()+weapon.getSpeed());

    }

//    private void saveWeapon(){
//        Weapon currentWeapon=new Weapon("weapon",progressBar1.getProgress(),progressBar2.getProgress(),progressBar3.getProgress());
//        Gson gson=new Gson();
//        String weaponJson=gson.toJson(currentWeapon);
//        Toast.makeText(this,weaponJson,Toast.LENGTH_SHORT).show();
//        FileOutputStream out=openFileOutput("weapon.txt",MODE_PRIVATE);
//        out.write(weaponJson.getBytes());
//        out.close();
//
//    }
//    private void readWeapon(){
//        FileInputStream in=openFileInput("weapon.txt");
//        byte[] buffer=new  byte[in.available()];
//        in.read(buffer);
//        String weaponJson=new String(buffer);
//    }
}
